import { stringify } from 'qs';
import _ from 'lodash';
import request, { checkResultCode, checkStatus } from '../utils/request';
import { Constants } from '../utils';
import { ipConfig } from './config';

// http://mesdev.chalco-steering.com:1708/appgateway/api-ms/pub/todo/search?appId=APP_MES&status=2&page=9&rows=10&type=1

// export const serverName = '/api-pea-device-170236';
export const serverName = ipConfig.baseServerName();

const imageUrl = `${serverName}/logistics/fileController/download/`; // 图片路径

const queryCheckEquipmentUrl = `${serverName}/materialDefinition/lists`; // 查询备件列表

const querySpartPartInfoUrl = `${serverName}/materialDefinition/infos`; // 查询备件详情

const queryInOrOutBoundInfoUrl = `${serverName}/stockLogCtrl/logs/info`; // 查询出入库详情

const queryInOrOutBoundListUrl = `${serverName}/stockLogCtrl/logs`; // 查询出入库列表

const queryMaterialDefinitionByLotCodeUrl = `${serverName}/materialDefinition/byLotCode`; // 查询物料

const submitOutBoundUrl = `${serverName}/stockLogCtrl/outWarehouse`; // 新增出库

const careUrl = `${serverName}/device/attention/save`; // 关注

const cancelCareUrl = `${serverName}/device/attention/cancel`; // 取消关注

export const getDownloadImg = `${ipConfig.url(`${serverName}/logistics/fileController/download`)}`;

export const querySpartPartList = async (params) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryCheckEquipmentUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/materialDefinition/lists?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapQuerySpartPartList);
};

export const queryInOrOutBoundList = async (params) => {
  const defaultParams = { rows: 4, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryInOrOutBoundListUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/stockLogCtrl/logs?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapQueryInOrOutBoundList);
};

export const querySpartPartListSearch = async (params) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryCheckEquipmentUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/materialDefinition/lists?${stringify(newParams)}`, {
    method: 'GET',
  }).then();
};

// export const queryInOrOutBoundListSearch = async (params) => {
//   const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
//   const newParams = { ...defaultParams, ...params };
//   // return request(`${ipConfig.url(queryInOrOutBoundListUrl)}?${stringify(newParams)}`, {
//   return request(`http://192.168.15.56:80/stockLogCtrl/logs?${stringify(newParams)}`, {
//     method: 'GET',
//   }).then();
// };

export const querySpartPartInfo = async (params) => {
  const newParams = { ...params };
  return request(`${ipConfig.url(querySpartPartInfoUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/materialDefinition/infos?${stringify(newParams)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const queryInOrOutBoundInfo = async (params) => {
  const newParams = { ...params };
  return request(`${ipConfig.url(queryInOrOutBoundInfoUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/stockLogCtrl/logs/info?${stringify(newParams)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const queryMaterialDefinitionByLotCode = async (params) => {
  const newParams = { ...params };
  return request(`${ipConfig.url(queryMaterialDefinitionByLotCodeUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/materialDefinition/byLotCode?${stringify(newParams)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const subOutBound = async params => {
  return request(`${ipConfig.url(submitOutBoundUrl)}`, {
  // return request('http://192.168.15.56:80/stockLogCtrl/outWarehouse', {
    method: 'POST',
    body: params,
  }).then(response => response.data);
};

export const care = async params => {
  return request(`${ipConfig.url(careUrl)}?${stringify(params)}`, {
  // return request('http://192.168.15.56:80/stockLogCtrl/outWarehouse', {
    method: 'POST',
    body: {},
  }).then();
};

export const cancelCare = async params => {
  return request(`${ipConfig.url(cancelCareUrl)}?${stringify(params)}`, {
  // return request('http://192.168.15.56:80/stockLogCtrl/outWarehouse', {
    method: 'POST',
    body: {},
  }).then();
};

const mapQuerySpartPartList = response => {
  const data = _.map(response.data, it => ({
    materialName: it.materialName,
    materialSpecModel: it.materialSpecModel,
    materialCode1: it.materialCode1,
    materialId: it.materialId,
    id: it.id,
    fisrtQuantity: it.fisrtQuantity == null ? '0' : it.fisrtQuantity,
    care: it.flag == '0',
    failurePic: it.photos ? `${ipConfig.url(imageUrl)}${it.photos}?token=${global.__TOKEN_ACCESS__}` : 'no-pic',
  }));

  return {
    list: data,
    page: response.pageNum,
    total: response.totalNum,
  };
};

const mapQueryInOrOutBoundList = response => {
  return {
    list: response.data,
    page: response.pageNum,
    total: response.totalNum,
  };
};
